package com.muqi.app.user.widget;

import java.util.List;

import com.muqi.app.project.utils.RecordTypeProvider;
import com.muqi.app.qlearn.modles.RecordTypeBean;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.address.ScrollerNumberPicker;
import com.muqi.app.user.address.ScrollerNumberPicker.OnSelectListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class RecordTypePicker extends LinearLayout {
	/** 滑动控件 */
	private ScrollerNumberPicker provincePicker;
	private ScrollerNumberPicker cityPicker;
	private ScrollerNumberPicker counyPicker;
	/** 选择监听 */
	private OnSelectingListener onSelectingListener;
	/** 刷新界面 */
	private static final int REFRESH_VIEW = 0x001;
	/** 临时日期 */
	private int tempProvinceIndex = 0;
	private int temCityIndex = 0;
	private int tempCounyIndex = 0;
	private RecordTypeBean selecter = null;

	public RecordTypePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public RecordTypePicker(Context context) {
		super(context);
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		LayoutInflater.from(getContext()).inflate(R.layout.city_picker2, this);
		provincePicker = (ScrollerNumberPicker) findViewById(R.id.province);
		cityPicker = (ScrollerNumberPicker) findViewById(R.id.city);
		counyPicker = (ScrollerNumberPicker) findViewById(R.id.couny);
		// 获取控件引用
		provincePicker.setData(RecordTypeProvider.getFirstLeverNames());
		provincePicker.setDefault(0);
		cityPicker.setData(RecordTypeProvider.getSecondLeverNames());
		cityPicker.setDefault(0);
		counyPicker.setData(RecordTypeProvider.getThirdLeverNames(0));
		counyPicker.setDefault(0);
		
		provincePicker.setOnSelectListener(new OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				
				
				tempProvinceIndex = id;
				Message message = new Message();
				message.what = REFRESH_VIEW;
				handler.sendMessage(message);
			}

			@Override
			public void selecting(int id, String text) {
				
			}
		});
		cityPicker.setOnSelectListener(new OnSelectListener() {

			@Override
			public void endSelect(int id, String text) {
				if (text.equals("") || text == null)
					return;
				if (temCityIndex != id) {
					String selectDay = provincePicker.getSelectedText();
					if (selectDay == null || selectDay.equals(""))
						return;
					String selectMonth = counyPicker.getSelectedText();
					if (selectMonth == null || selectMonth.equals(""))
						return;
					counyPicker.setData(RecordTypeProvider.getThirdLeverNames(id));
					counyPicker.setDefault(0);
					tempCounyIndex = 0;
					int lastDay = Integer.valueOf(cityPicker.getListSize());
					if (id > lastDay) {
						cityPicker.setDefault(lastDay - 1);
					}
				}
				
				temCityIndex = id;
				Message message = new Message();
				message.what = REFRESH_VIEW;
				handler.sendMessage(message);
			}

			@Override
			public void selecting(int id, String text) {

			}
		});
		counyPicker.setOnSelectListener(new OnSelectListener() {
			
			@Override
			public void endSelect(int id, String text) {
				if (text.equals("") || text == null)
					return;
				if (tempCounyIndex != id) {
					String selectDay = provincePicker.getSelectedText();
					if (selectDay == null || selectDay.equals(""))
						return;
					String selectMonth = cityPicker.getSelectedText();
					if (selectMonth == null || selectMonth.equals(""))
						return;
					int lastDay = Integer.valueOf(counyPicker.getListSize());
					if (id > lastDay) {
						counyPicker.setDefault(lastDay - 1);
					}
				}
				
				tempCounyIndex = id;
				Message message = new Message();
				message.what = REFRESH_VIEW;
				handler.sendMessage(message);
			}
			
			@Override
			public void selecting(int id, String text) {
				
			}
		});
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case REFRESH_VIEW:
				if (onSelectingListener != null)
					onSelectingListener.selected(true);
				break;
			default:
				break;
			}
		}
	};
	
	public RecordTypeBean getSelectedModle(){
		List<RecordTypeBean> childList = RecordTypeProvider.recordTypeLists.get(temCityIndex).childList;
		if(childList!=null){
			if(tempCounyIndex < childList.size()){
				selecter = childList.get(tempCounyIndex);
			}else{
				selecter = childList.get(0);
			}
		}
		return selecter;
	}

	public void setOnSelectingListener(OnSelectingListener onSelectingListener) {
		this.onSelectingListener = onSelectingListener;
	}

	public interface OnSelectingListener {
		public void selected(boolean selected);
	}
}
